//
//  ViewController.swift
//  CustomInsetsLabel
//
//  Created by 李煜 on 2024/9/12.
//

import SnapKit
import UIKit

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    testInsetsLabel()
    testInsetsLabelWithSnapKit()
    testQMUILabelWithSnapKit()
//    testFeedLine()
//    testMultiFeedLine()
    
  }

  func testFeedLine() {//https://www.jianshu.com/p/bdfacbe74eff
    let text = "要显示的文字"
    let font = UIFont.systemFont(ofSize: 20)
    let maxWidth: CGFloat = 200

    let attributedString = NSMutableAttributedString(string: text)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byWordWrapping
    paragraphStyle.alignment = .center

    let range = NSRange(location: 0, length: text.count)
    attributedString.addAttribute(.font, value: font, range: range)
    attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)

    let boundingRect = attributedString.boundingRect(with: CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
    let height = ceil(boundingRect.height)

    let label = UILabel(frame: CGRect(x: 0, y: 70, width: maxWidth, height: height))
    label.numberOfLines = 0
    label.attributedText = attributedString

    view.addSubview(label)
  }

  func testMultiFeedLine() {
    let text = "要显示的文字要显sfas示的文字sgaadsgasg要显hsh示的文hahdsh字要显示的文字要显示的文字要显hdfhdfh示的文字hfdhd要显示的文字"
    let font = UIFont.systemFont(ofSize: 20)
    let maxWidth: CGFloat = 200

    let attributedString = NSMutableAttributedString(string: text)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byWordWrapping
    paragraphStyle.alignment = .center

    let range = NSRange(location: 0, length: text.count)
    attributedString.addAttribute(.font, value: font, range: range)
    attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)

    let boundingRect = attributedString.boundingRect(with: CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
    let height = ceil(boundingRect.height)

    let label = UILabel(frame: CGRect(x: 0, y: 200, width: maxWidth, height: height))
    label.numberOfLines = 0
    label.attributedText = attributedString

    view.addSubview(label)
  }

  func testInsetsLabel() {
    let lb = InsetsLabel(frame: .init(x: 10, y: 70, width: lbWidth, height: 0))
    lb.insets = commonInsets
    lb.backgroundColor = .orange
    lb.text = "abcdSqweuotperotpermnxv,xvk"
    lb.numberOfLines = 0
    lb.textColor = .black
    lb.lineBreakMode = lineBreakMode
    view.addSubview(lb)
    lb.sizeToFit()
    lb.tag = 0
    
    let rightTopLb = InsetsLabel()
    view.addSubview(rightTopLb)
    rightTopLb.insets = commonInsets
    rightTopLb.backgroundColor = .systemPink
    rightTopLb.text = "abcdSqweuotperotpermnxv,xvk"
    rightTopLb.numberOfLines = 0
    rightTopLb.textColor = .black
    rightTopLb.lineBreakMode = lineBreakMode
    rightTopLb.snp.makeConstraints { make in
      make.top.equalTo(lb)
      make.left.equalTo(lb.snp.right).offset(10)
      make.width.equalTo(lbWidth)
    }
    rightTopLb.tag = 1
  }
  var commonInsets: UIEdgeInsets = UIEdgeInsets(top: 5, left: 15, bottom: 20, right: 5)
//  var commonInsets: UIEdgeInsets = .zero
  var lineBreakMode: NSLineBreakMode = .byCharWrapping
  var lbWidth: CGFloat = 70
  // TODO: 待解决
  // 带insets的label使用snapkit设置宽度后,无法保证高度能足够文字显示完全
  func testInsetsLabelWithSnapKit() {
    let lb = InsetsLabel()
    view.addSubview(lb)
    lb.insets = commonInsets
    lb.backgroundColor = .purple
    lb.text = "abcdSqweuotperotpermnxv,xvk"
    lb.numberOfLines = 0
    lb.textColor = .black
    lb.lineBreakMode = lineBreakMode
    lb.snp.makeConstraints { make in
      make.top.equalTo(200)
      make.left.equalTo(200)
      make.width.equalTo(lbWidth)
    }
    lb.tag = 2
  }
  func testQMUILabelWithSnapKit() {
    
    let lb = InsetsLabel()
    view.addSubview(lb)
    
    lb.insets = commonInsets
    lb.backgroundColor = .systemPink
    lb.text = "abcdSqweuotperotpermnxv,xvk"
    lb.numberOfLines = 0
    lb.textColor = .black
    lb.lineBreakMode = lineBreakMode
    lb.snp.makeConstraints { make in
      make.top.equalTo(200)
      make.left.equalTo(10)
      make.width.equalTo(lbWidth)
    }
    lb.tag = 2
    
    let lb2 = QMUILabel()
    view.addSubview(lb2)
    lb2.contentEdgeInsets = commonInsets
//    lb2.insets = commonInsets
    lb2.backgroundColor = .blue
    lb2.textColor = .white
    lb2.text = "abcdSqweuotperotpermnxv,xvk"
    lb2.numberOfLines = 0
    lb2.textColor = .white
    lb2.lineBreakMode = lineBreakMode
    lb2.snp.makeConstraints { make in
      make.top.equalTo(200)
      make.left.equalTo(lb.snp.right).offset(10)
      make.width.equalTo(lbWidth)
    }
    lb2.tag = 3
    
    let lb3 = QMUILabel()
    view.addSubview(lb3)
    lb3.contentEdgeInsets = commonInsets
    lb3.backgroundColor = .brown
    lb3.textColor = .white
    lb3.text = "abcdSqweuotperotpermnxv,xvk"
    lb3.numberOfLines = 0
    lb3.textColor = .white
    lb3.frame = CGRectMake(10, 400, 100, 40)
    lb3.sizeToFit()
    lb3.tag = 4
    
  }
}
